package com.kaibes.platform.currency.api;

import java.math.BigInteger;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kaibes.platform.currency.db.CurrencyUser;
import com.kaibes.platform.currency.db.CurrencyUserService;
import com.kaibes.platform.currency.pojo.CurrencyUserUpdate;
import com.kaibes.web.api.ApiLink;
import com.kaibes.web.api.ResponseApi;
import com.kaibes.web.response.ResponseFactory;

@RestController
@RequestMapping("/currencys/users")
public class CurrencyUserApi extends ResponseApi<CurrencyUser, CurrencyUserService> {

    @PutMapping("/{id:\\d+}")
    @ApiLink("修改货币持有，根据id")
    public ResponseEntity<Object> putById(@PathVariable BigInteger id, @RequestBody CurrencyUser data) {
        return super.putById(data, id, "修改货币成功", "修改货币失败");
    }
    
    @PutMapping
    @ApiLink("修改货币持有，根据id")
    public ResponseEntity<Object> putByUserId(@RequestBody CurrencyUserUpdate data) {
        CurrencyUser currencyUser = service.getByUserIdAndCurrencyId(data.getUserId(), data.getCurrencyId());
        if (currencyUser == null) {
            currencyUser = new CurrencyUser();
            currencyUser.setCurrencyId(data.getCurrencyId());
            currencyUser.setUserId(data.getUserId());
            currencyUser.setAmount(data.getAmount());
            return ResponseFactory.post(service.save(currencyUser), "修改货币持有成功", "修改货币持有失败");
        } else {
            return ResponseFactory.put(service.updateAmountByUserIdAndCurrencyId(data), "修改货币持有成功", "修改货币持有失败");
        }
    }
    
}
